package main.Link;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by s_shmakov on 26.03.14.
 */
public class CellRendererTextArea extends JTextArea implements TableCellRenderer {

    private String rrow;

    public CellRendererTextArea(String rrow) {
        super();
        this.rrow = rrow;
        setLineWrap(true);
        setWrapStyleWord(true);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object arg1, boolean isSelected, boolean hasFocus, int row, int column) {
        if (table.getValueAt(row, 0).equals(rrow)) {
            super.setBackground(isSelected ? SystemColor.textHighlight : new Color(175, 227, 193));
        } else {
            super.setBackground(isSelected ? SystemColor.textHighlight : new Color(255,255,255));
        }
        super.setForeground(isSelected ? Color.white : Color.black);
        String data;
        if (arg1 == null) {
            data = "";
        } else {
            data = (String) arg1.toString();
        }
        int lineWidth = this.getFontMetrics(this.getFont()).stringWidth(data);
        int lineHeight = this.getFontMetrics(this.getFont()).getHeight();
        int rowWidth = table.getCellRect(row, column, true).width;

        int newRowHeight = (int) ((lineWidth / rowWidth) * (lineHeight)) + lineHeight * 2;
        if (table.getRowHeight(row) < newRowHeight) {
            table.setRowHeight(row, newRowHeight);
        }
        this.setText(data);
        this.setFont(new Font("Calibri", 0, 12));
        return this;
    }
}
